迭代：
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (!head) return head;
        ListNode *cur = head, *prev = nullptr;
        while (cur) {
            ListNode *tmp = cur->next;
            cur->next = prev;
            prev = cur; 
            cur = tmp;
        }
        return prev;
    }
};

递归：
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverse(ListNode *cur, ListNode* prev) {
        if (cur == nullptr) {
            return prev;
        } 
        ListNode* res = reverse(cur->next, cur);
        // prev = cur;
        cur->next = prev;
        return res;
    }
    ListNode* reverseList(ListNode* head) {
        return reverse(head, nullptr);
    }
};